package com.example.administrator.demo1;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hxb.deviceservice.aidl.IDeviceService;
import com.hxb.deviceservice.aidl.printer.IPrinter;
import com.hxb.deviceservice.aidl.printer.OnPrintListener;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import print.BitmapUtil;

import static android.R.attr.offset;

public class PrintActivity extends AppCompatActivity implements View.OnClickListener {


    private IPrinter iPrinter;
    private IBinder printer;
    private ServiceConnection sc = new ServiceConnection() {


        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IDeviceService deviceService = IDeviceService.Stub.asInterface(service);
            try {
                printer = deviceService.getPrinter();
                iPrinter = IPrinter.Stub.asInterface(printer);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("没找到SERVICE                " + name);
        }
    };


    private OnPrintListener onPrintListener = new OnPrintListener.Stub() {

        @Override
        public void onFinish() throws RemoteException {
//            Toast.makeText(PrintActivity.this, "打印成功", Toast.LENGTH_SHORT).show();
            System.out.println("print success ");
        }

        @Override
        public void onError(int i) throws RemoteException {
            System.out.println("print Lose         code " + i);
        }
    };


    private Button mText;
    private Button mImg;
    private Button mBarCode;
    private Button mQr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print);

        init();


        mText = (Button) findViewById(R.id.print_text);
        mImg = (Button) findViewById(R.id.print_img);
        mBarCode = (Button) findViewById(R.id.print_barcode);
        mQr = (Button) findViewById(R.id.print_qr);
        mText.setOnClickListener(this);
        mImg.setOnClickListener(this);
        mBarCode.setOnClickListener(this);
        mQr.setOnClickListener(this);
    }

    /**
     * 初始化打印接口
     */
    private void init() {
        Intent intent = new Intent();
        intent.setPackage("com.hxb.deviceservice");//兼容Android 5.0
        //intent.setPackage("com.zacloud.deviceservice");
        intent.setAction("com.hxb.device_service");
        boolean b = bindService(intent, sc, Service.BIND_AUTO_CREATE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.print_text:
//文字
                try {
                    printData("s");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.print_img:
//图片
                Resources res = this.getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.test);
                byte[] bytes = BitmapUtil.bmp2bytesStream(bmp);

                try {
                    printImg(bytes);
                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.print_barcode:
//条码
                try {
                    printBarCode("44444444582");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.print_qr:
//二维码
                try {
                    printQrCode("https://www.baidu.com/?tn=94462257_hao_pg");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }

    }

    /**
     * 打印二维码
     *
     * @param QrCode 二维码数据
     * @throws RemoteException
     */
    private void printQrCode(String QrCode) throws RemoteException {
        Bundle bundle = new Bundle();
        bundle.putInt("expectedHeight", 256);
        bundle.putInt("align", 1);
        iPrinter.addQrCode(bundle, QrCode);
        iPrinter.feedLine(4);
        iPrinter.startPrint(onPrintListener);
    }

    /**
     * 打印条码
     *
     * @param barcode 需打印条码的数据
     * @throws RemoteException
     */
    private void printBarCode(String barcode) throws RemoteException {
        Bundle bundle = new Bundle();
        bundle.putInt("codeWidth", 7);
        bundle.putInt("codeHeight", 32);
        bundle.putInt("align", 1);
        iPrinter.addBarCode(bundle, barcode);
        iPrinter.feedLine(4);
        iPrinter.startPrint(onPrintListener);
    }

    public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    /**
     * 打印图片
     *
     * @param imgData 图片数据
     * @throws RemoteException
     */
    public void printImg(byte[] imgData) throws RemoteException, IOException {
        InputStream ins = this.getAssets().open("seuic.bmp");
        Bitmap bmp = BitmapFactory.decodeStream(ins);
        byte[] image = Bitmap2Bytes(bmp);
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        width = 0 == width % 8 ? width : width + (8 - width % 8);

        Bundle imageBun = new Bundle();
        imageBun.putInt("offset", 0);
        imageBun.putInt("width", width);
        imageBun.putInt("height", height);
        iPrinter.addImage(imageBun, image);
        iPrinter.feedLine(3);
        iPrinter.startPrint(onPrintListener);
    }


    /**
     * 打印字符
     *
     * @param data 需要打印的byte数组
     * @throws RemoteException
     */
    public void printData(String data) throws RemoteException {

        Bundle bundle = new Bundle();
        bundle.putInt("font", 1);
        bundle.putInt("align", 0);
        iPrinter.addText(bundle, "====================打印");
        iPrinter.addText(bundle, "====================打印");
        iPrinter.addText(bundle, "====================打印");
        iPrinter.addText(bundle, "====================打印");
        iPrinter.addText(bundle, "====================打印");
        iPrinter.feedLine(4);

        iPrinter.startPrint(onPrintListener);

    }

}
